package com.imooc.miaosha.controller;

import com.imooc.miaosha.domain.MiaoShaUser;
import com.imooc.miaosha.domain.OrderInfo;
import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.vo.GoodsVo;
import com.imooc.miaosha.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/detail")
    @ResponseBody
    public Result<OrderDetailVo> info(MiaoShaUser user,
                                      @RequestParam("orderId") long orderId){

        if(user == null) return Result.error(CodeMsg.SESSION_ERROR);

        OrderInfo orderInfo = orderService.getOrderById(orderId);
        if(orderInfo == null) return Result.error(CodeMsg.ORDER_NOT_EXIST);
        Long goodsId = orderInfo.getGoodsId();
        GoodsVo goodsVo = goodsService.getGoodsVoByGoodsId(goodsId);

        OrderDetailVo detailVo = new OrderDetailVo();
        detailVo.setGoods(goodsVo);
        detailVo.setOrder(orderInfo);
        detailVo.setUser(user);

        return Result.success(detailVo);
    }
}
